<?php
/**
 * Short description for categories_controller.php
 *
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_blogs_plugin
 * @subpackage    lil_blogs_plugin.controllers
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * CategoriesController class
 *
 * @uses          LilBlogsAppController
 * @package       lil_blogs_plugin
 * @subpackage    lil_blogs_plugin.controllers
 */
class CategoriesController extends LilBlogsAppController {
/**
 * name property
 *
 * @var string 'Categories'
 * @access public
 */
	var $name = 'Categories';
/**
 * uses property
 *
 * @var array
 * @access public
 */
	var $uses = array('LilBlogs.Category');
/**
 * beforeFilter method
 *
 * @access public
 * @return void
 */
	function beforeFilter() {
		$this->Auth->allow('index');
		parent::beforeFilter();
	}
/**
 * isAuthorized method
 *
 * @access public
 * @return bool
 */
	function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		if (isset($this->Auth) && !Configure::read('Lil.allowUsersAnything')) {
			// blog has to be selected
			// get selected blog_id
			
			if (
				(!$blog_id = $this->getAdminAreaId()) && 
				(!Configure::read('LilBlogs.allowEmptyBlog'))
			) return false;
			
			
			// check if category belongs to current blog
			if (in_array($this->params['action'], array('admin_edit', 'admin_delete'))) {
				if (empty($this->params['pass'][0])) return false;
				
				$cat_blog_id = $this->Category->field(
					'blog_id',
					array('Category.id' => $this->params['pass'][0])
				);
				
				if (!empty($blog_id) && ($blog_id != $cat_blog_id)) {
					return false;
				}
				
				$blog_id = $cat_blog_id;
			}
			
			return
				$this->isAdmin() ||
				$this->User->canAccessArea($this->Auth->user('id'), $blog_id);
		}
		return true;
	}
/**
 * categories method
 *
 * @access public
 * @return bool
 */
	function index() {
		$category_id = null;
		if (!empty($this->params['category_id'])) $category_id = $this->params['category_id'];
		
		// apply seatch criterio
		$categories = $this->Category->children($this->params['category_id'], true, null, null, null, null, 1);
		$parent = $this->Category->find('first', array(
			'conditions' => array('Category.id' => $this->params['category_id'])
		));
		$this->set(compact('parent', 'categories'));
	}
/**
 * admin_index method
 *
 * @access public
 * @return void
 */
	function admin_index() {
		$blog_id = $this->getAdminAreaId();
		if (empty($blog_id) && !empty($this->params['named']['blog'])) {
			if ($bid = $this->findBlogBySlug($this->params['named']['blog'])) {
				$blog_id = $bid['Blog']['id'];
			} else {
				$blog_id = false;
			}
		}
		
		$this->paginate = array(
			'conditions' => array('Category.blog_id' => $blog_id),
			'order'      => 'Category.lft',
			'contain'    => array()
		);
		
		$this->Category->recursive = -1;
		$this->set('data', $this->paginate('Category'));
	}
/**
 * admin_add method
 *
 * @param int $blogid
 * @access public
 * @return void
 */
	function admin_add() {
		$blog_id = $this->getAdminAreaId();
		if (empty($blog_id) && !empty($this->params['named']['blog'])) {
			if ($bid = $this->findBlogBySlug($this->params['named']['blog'])) {
				$blog_id = $bid['Blog']['id'];
			} else {
				$blog_id = false;
			}
		}
		
		if (!empty($this->data)) {
			$this->Category->create();
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__d('lil_blogs', 'A new category has been created.', true));
				$this->doRedirect(array('admin' => true, 'action' => 'index'));
			} else {
				$this->Session->setFlash(__d('lil_blogs', 'Please verify that the information is correct.', true), 'error');
			}
		} else {
			$this->data['Category']['blog_id'] = $blog_id;
			$this->setupRedirect();
		}
		
		// generate parent list
		$p = $this->Category->generateTree('both', array(
			'conditions' => array('Category.blog_id' => $blog_id)
		));
		$this->set('parents', $p['simple']);
		$this->set('tree', $p['threaded']);
	}
/**
 * admin_edit method
 *
 * @param int $id
 * @access public
 * @return void
 */
	function admin_edit($id=null) {
		$blog_id = $this->getAdminAreaId();
		if (empty($blog_id) && !empty($this->params['named']['blog'])) {
			if ($bid = $this->findBlogBySlug($this->params['named']['blog'])) {
				$blog_id = $bid['Blog']['id'];
			} else {
				$blog_id = false;
			}
		}
		
		$this->Category->recursive = -1;
		
		if (!empty($this->data)) {
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__d('lil_blogs', 'Category has been saved.', true));
				$this->doRedirect(array('admin' => true, 'action' => 'index'));
			} else {
				$this->Session->setFlash(__d('lil_blogs', 'Please verify that the information is correct.', true), 'error');
			}
		} else if (!is_numeric($id) || !($this->data = $this->Category->read(null, $id))) {
			$this->error404();
		}
		
		$this->setupRedirect();
		
		// generate parent list
		$p = $this->Category->generateTree('both', array(
			'conditions' => array('Category.blog_id' => $blog_id)
		));
		$this->set('parents', $p['simple']);
		$this->set('tree', $p['threaded']);
	}
/**
 * admin_delete method
 *
 * @param int $id
 * @access public
 * @return void
 */
	function admin_delete($id=null) {
		$this->Category->recursive = -1;
		if (is_numeric($id) && $data = $this->Category->findById($id)) {
			$this->Category->delete($id);
			$this->Session->setFlash(__d('lil_blogs', 'Category has been deleted.', true));
			$this->redirect($this->referer());
		} else {
			$this->error404();
		}
	}
}